<?php

namespace App\Console\Commands;

use App\model\panyuan\WorkshopOverdue;
use App\model\Workshop;
use Illuminate\Console\Command;
use Illuminate\Support\Carbon;

class WorkshopExpire extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'workshop:expire';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '将要过期的厂房';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        Workshop::withoutGlobalScope('shenhe')->chunk(100,function ($workshops){
            foreach ($workshops as  $workshop){

               if ( Carbon::parse($workshop->commission_end_day)->lt(Carbon::now()->addDays(30))){
                   $this->info('过期'.$workshop->id);
                   $workshopOverdue= WorkshopOverdue::where('workshop_id',$workshop->id)->where('is_shenhe',WorkshopOverdue::NO_SHENHE)->first();
                   if (!$workshopOverdue){
                     $workshopOverdue=  new WorkshopOverdue();
                     $workshopOverdue->user_id=$workshop->user_id;
                     $workshopOverdue->overdue_time=$workshop->commission_end_day;
                     $workshopOverdue->workshop_id=$workshop->id;
                     $workshopOverdue->role_id=1;
                     $workshopOverdue->is_updated=WorkshopOverdue::NO_UPDATED;
                     $workshopOverdue->is_shenhe=WorkshopOverdue::NO_SHENHE;
                     $workshopOverdue->save();
                   }
               }
            }
        });
    }
}
